package org.ithang.application.invest.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.Logger;
import org.ithang.application.account.bean.Account;
import org.ithang.application.account.service.AccountService;
import org.ithang.application.customer.service.CustomerService;
import org.ithang.application.grade.bean.Grade;
import org.ithang.application.grade.service.GradeService;
import org.ithang.application.invest.bean.Invest;
import org.ithang.application.invest.mapper.InvestMapper;
import org.ithang.application.teamBonus.bean.TeamBonus;
import org.ithang.application.teamBonus.service.TeamBonusService;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.database.ModelDao;
import org.ithang.tools.lang.DateUtils;
import org.ithang.tools.lang.GradeUtil;
import org.ithang.tools.model.Arith;
import org.ithang.tools.model.Pager;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 投资表 服务
 * @author zyt
 *
 */
@Service
public class InvestService extends ModelDao<Invest>{

    @Autowired
    private InvestMapper investMapper;

    @Autowired
    private AccountService accountService;
    
    @Autowired
    private CustomerService customerService;
    
    @Autowired
    private KeyvalueService keyvalueService;
    
    @Autowired
    private TeamBonusService teamBonusService;
    
    @Autowired
    private GradeService gradeService;
    
    private Logger logger = Logger.getLogger(InvestService.class);
    
    /**
     * 投资
     * @param amount
     * @param account
     */
    @Transactional
    public synchronized void doInvest(double amount,Account account)throws Exception{
    	Invest invest=new Invest();
		invest.setCurrency_id(1);
		invest.setAmount(amount);
		invest.setCustomer_id(account.getCustomer_id());
		invest.setCreate_time(DateUtils.getSystime());
		add(invest);//新增投资记录
		
		account.setAmount(Arith.sub(account.getAmount(),amount));//可用金额-amount
		account.setInvest_amount(Arith.add(account.getInvest_amount(),amount));//个人投资+amount
		account.setTeam_invest_amount(Arith.add(account.getTeam_invest_amount(),amount));//团队投资+amount
		accountService.update(account);
		
		addTeambonus(account.getFrom_user(),amount);
		
		int fromuser=account.getFrom_user()==null?0:account.getFrom_user();
		while(fromuser>0){
			account=accountService.get(fromuser);
			account.setTeam_invest_amount(Arith.add(account.getTeam_invest_amount(),amount));//团队投资+amount
			accountService.update(account);
			fromuser=account.getFrom_user();
		}
		
    }
    
    /**
     * fromUser 上级用户ID
     * amount 投资金额
     * 动态加速
     */
    private void addTeambonus(Integer fromUser,double amount){
    	if(null!=fromUser&&fromUser>0){
			//动态分润
			String value=keyvalueService.get(Keys.IsAddTeamBonus).getValue();
			if(null!=value&&"1".equals(value.trim())){
				Map<String,Object> condition=new HashMap<>(5);
				condition.put("type", 1);
				List<Grade> grades=gradeService.query(condition);
				Account fromAccount=accountService.get(fromUser);
				
				if(null==fromAccount.getInvest_amount()){
					fromAccount.setInvest_amount(0D);
				}
				if(null==fromAccount.getTeam_bonus()){
					fromAccount.setTeam_bonus(0D);	
				}
				if(null==fromAccount.getAddteam_bonus()){
					fromAccount.setAddteam_bonus(0D);
				}
				
				//if(Arith.mul(fromAccount.getInvest_amount(),2D)>fromAccount.getAddteam_bonus()){//如果 团队收益的小于个人投资本金的2倍 则继续分红
					Double rate=GradeUtil.getRate(grades, fromAccount.getTeam_invest_amount()==null?0:fromAccount.getTeam_invest_amount())/100D;
					
					if(rate>0){
						Double bouns=Arith.mul(amount,rate);
						
						TeamBonus teamBonus=new TeamBonus();
						teamBonus.setAmount(bouns);
						teamBonus.setType(1);
						teamBonus.setCustomer_id(fromAccount.getCustomer_id());
						teamBonus.setCreate_time(DateUtils.getSystime());
						teamBonusService.add(teamBonus);//添加分润记录
						
						fromAccount.setAmount(Arith.add(fromAccount.getAmount(),bouns));
						fromAccount.setAddteam_bonus(Arith.add(fromAccount.getAddteam_bonus(),bouns));
						accountService.update(fromAccount);
					}
					
				//}
				
			}
		}
    }
    
    
    public void add(Invest invest){
        investMapper.add(invest);
    }
    
    public Invest get(Integer id){
        return investMapper.get(id);
    }
    
    public int delete(Integer id){
        return investMapper.delete(id);
    }
    
    public int batchDelete(String[] ids){
    	return investMapper.batchDelete(ids);
    }
    
    public List<Invest> list(@Param("ids")Integer... ids){
    	return investMapper.list(ids);
    }
    
    public List<Invest> listByAccount(int account_id){
    	return investMapper.listByAccount(account_id);
    }
    
    public List<Invest> page(Pager<Invest> page){
    	return investMapper.page(page);
    }
    
    public List<Invest> query(Map<String,Object> conditions){
    	return investMapper.query(conditions);
    }
    
    public int update(Invest invest){
    	return investMapper.update(invest);
    }
    
    
}